package com.liuyk.commonlib.utility;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * json数据的解析
 * <p>
 * Created by liuyakui on 2020/2/05.
 */
public final class JsonUtils {

    /**
     * 解析本地文件json格式数据
     *
     * @param context context
     * @param clazz   class
     * @param name    assets文件夹中json文件名
     * @param <T>     bean
     * @return bean
     */
    public static <T> T parseJson(Context context, Class<T> clazz, String name) {
        try {
            InputStream in = context.getResources().getAssets().open(name);
            return parseJson(clazz, getString(in));
        } catch (IOException e) {
            return null;
        }
    }

    public static <T> List<T> parseJson(Context context, String fileName, Class<T[]> clazz) {
        try {
            final InputStream in = context.getResources().getAssets().open(fileName);
            final String jsonText = new String(getString(in));
            final Gson gson = new Gson();
            List<T> list;

            if (!TextUtils.isEmpty(jsonText)) {
                final T[] t = gson.fromJson(jsonText, clazz);
                list = Arrays.asList(t);
                return list;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] getString(InputStream in) {
        ByteArrayOutputStream byo = new ByteArrayOutputStream();
        try {
            byte[] buff = new byte[1024];
            int len;
            while ((len = in.read(buff)) != -1) {
                byo.write(buff, 0, len);
                byo.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                try {
                    in.close();
                    byo.close();
                } catch (IOException ie) {
                    Log.i("value", "IOException" + ie.toString());
                }
        }
        return byo.toByteArray();
    }

    public static <T> List<T> parseJson(String jsonText, Class<T[]> clazz) {
        final Gson gson = new Gson();
        List<T> list;
        if (!TextUtils.isEmpty(jsonText)) {
            final T[] t = gson.fromJson(jsonText, clazz);
            list = Arrays.asList(t);
            return list;
        }
        return null;
    }

    /**
     * 解析json
     *
     * @param type
     * @param data
     * @param <T>
     * @return
     */
    public static <T> T parseJson(Class<T> type, byte[] data) {
        final Gson gson = new Gson();
        T result;
        try {
            result = gson.fromJson(new String(data), type);
        } catch (Exception e) {
            result = null;
        }
        return result;
    }

    public static <T> T parseJson(Class<T> type, String data) {
        final Gson g = new Gson();
        try {
            return g.fromJson(data, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String toJsonText(Object req) {
        String reqJson = "";
        try {
            Gson gson = new GsonBuilder().disableHtmlEscaping().create();
            reqJson = gson.toJson(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reqJson;
    }
}
